import java.io.IOException;

import RMI.Main;

public class ServiceControl {
    // static instance of the service class
	   private static ServiceControl serviceInstance = new ServiceControl();
	   // flag - if service instance has stopped
	   private boolean stopped = false; 
	   Main m = null;
	   // the method from which tomcat will start/stop the service 
	   public static void windowsService(String args[]) {
	      String cmd = "start";
	      if(args.length > 0) cmd = args[0];

	      if("start".equals(cmd))  serviceInstance.start();
	      else serviceInstance.stop();
	   }
	// starts the service instance
	   public void start() {
	      stopped = false;
	      System.out.println("My Service Started "+ new java.util.Date());
	      m = new Main();
	      m.start();
	      while(!stopped) {
	         System.out.println("My Service Executing "+new java.util.Date());
	         synchronized(this) {
	            try {
	               this.wait(60000);  // wait 1 minute
	            }
	            catch(InterruptedException ie){}
	         }
	      }
	      System.out.println("My Service Finished " + new java.util.Date());
	   }

	    //Stops this service instance
	   public void stop() {
		   m.stop();
		   stopped = true;
		   synchronized(this) {
			   this.notify();
		   }
	   }



}
